﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace SinaApi
{
    public class SinaBlogApiHelper
    {
        private CookieContainer cc = null;
        private string _userName;
        private string _password;

        public SinaBlogApiHelper()
        {
            cc = new CookieContainer();
        }

        public SinaBlogApiHelper(string userName, string password) : base()
        {
            _userName = userName;
            _password = password;

        }

        public string Get(string url)
        {
            var cache = new CredentialCache();
            cache.Add(new Uri(url), "Basic", new NetworkCredential(_userName, _password));
            var req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.CookieContainer = this.cc;
            req.Method = "GET";
            req.ContentType = "application/x-www-form-urlencoded";
            req.Referer = url;
            req.Credentials = cache;
            req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(string.Format("{0}:{1}", _userName, _password))));

            using (var response = (HttpWebResponse)req.GetResponse())
            {

                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码
                }
                var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                string data = reader.ReadToEnd();
                return data;
            }

        }

        public string Post(string url, string param)
        {
            var cache = new CredentialCache();
            cache.Add(new Uri(url), "Basic", new NetworkCredential(_userName, _password));
            byte[] bs = Encoding.UTF8.GetBytes(param);
            var req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.CookieContainer = this.cc;
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bs.Length;
            req.Referer = url;
            req.Credentials = cache;
            req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(string.Format("{0}:{1}", _userName, _password))));

            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            }


            using (var response = (HttpWebResponse)req.GetResponse())
            {

                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码
                }
                var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                string data = reader.ReadToEnd();
                return data;
            }
        }

        public string Get(string url, string userName, string password)
        {
            CredentialCache cache = new CredentialCache();
            cache.Add(new Uri(url), "Basic", new NetworkCredential(userName, password));
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.CookieContainer = this.cc;
            req.Method = "GET";
            req.ContentType = "application/x-www-form-urlencoded";
            req.Referer = url;
            req.Credentials = cache;
            req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(string.Format("{0}:{1}", userName, password))));

            using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
            {

                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码
                }
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                string data = reader.ReadToEnd();
                return data;
            }
        }

        public string Post(string url, string param, string userName, string password)
        {
            var cache = new CredentialCache();
            cache.Add(new Uri(url), "Basic", new NetworkCredential(userName, password));
            byte[] bs = Encoding.UTF8.GetBytes(param);
            var req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.CookieContainer = this.cc;
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bs.Length;
            req.Referer = url;
            req.Credentials = cache;
            req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(string.Format("{0}:{1}", userName, password))));

            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
            }


            using (var response = (HttpWebResponse)req.GetResponse())
            {

                string encoding = response.ContentEncoding;
                if (encoding == null || encoding.Length < 1)
                {
                    encoding = "UTF-8"; //默认编码
                }
                var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                string data = reader.ReadToEnd();
                return data;
            }
        }

    }
}
